package j20251111;

public class Solution1 {
    int m,n;
    int[] dx={0,0,1,-1,-1,-1,1,1};
    int[] dy={1,-1,0,0,-1,1,-1,1};
    public char[][] updateBoard(char[][] board, int[] click) {
        m=board.length;
        n=board[0].length;
        int x=click[0],y=click[1];
        if(board[x][y]=='M'){
            board[x][y]='X';
            return board;
        }
        dfs(board,x,y);
        return board;
    }
    public void dfs(char[][] board,int i,int j){
        //计算周围的雷的个数
        int count=0;
        for(int k=0;k<8;k++){
            int x=i+dx[k],y=j+dy[k];
            if(x>=0&&x<m&&y>=0&&y<n&&board[x][y]=='M'){
                count++;
            }
        }
        //周围没有雷
        if(count==0){
            board[i][j]='B';
            for(int k=0;k<8;k++){
                int x=i+dx[k],y=j+dy[k];
                if(x>=0&&x<m&&y>=0&&y<n&&board[x][y]=='E'){
                    dfs(board,x,y);
                }
            }
        }else{//周围有雷
            board[i][j]=(char)('0'+count);
            return;
        }
    }
}
